package com.easy.product.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easy.common.bean.Product;
import com.easy.common.service.IProductService;
import com.easy.product.mapper.ProductMapper;
import com.mysql.cj.jdbc.result.UpdatableResultSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.List;

@Service
public class ProductService implements IProductService {
    @Autowired
    ProductMapper productMapper;

    @Override
    public Product insert(Product product) {
        productMapper.insert(product);
        Product result = this.getByID(product.getId());
        return result;
    }

    @Override
    public Product update(Product product) {
        productMapper.updateById(product);
        Product result = this.getByID(product.getId());
        return result;
    }

    @Override
    public int delete(Integer id) {
        int num = productMapper.deleteById(id);
        return num;
    }

    @Override
    public Product getByID(Integer id) {
        Product result = productMapper.selectById(id);
        return result;
    }

    @Override
    public IPage<Product> getListByPage(Page page, Product product) {
        QueryWrapper wrapper = new QueryWrapper(product);
        IPage<Product> result = (IPage<Product>) productMapper.selectPage(page,wrapper);
        return result;
    }

    @Override
    public List<Product> getList(Product product) {
        QueryWrapper wrapper = new QueryWrapper(product);
        List<Product> result = productMapper.selectList(wrapper);
        return result;
    }

    @Override
    public int setActive(Product product) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        if (product.getIsActive() == 0){
            updateWrapper.set("is_active",1);
        }else {
            updateWrapper.set("is_active",0);
        }
        updateWrapper.eq("id",product.getId());
        int result = productMapper.update(updateWrapper);
        return result;
    }
}
